package com.design.classroommanagement.manager

import com.design.classroommanagement.activity.LoginActivity
import com.design.classroommanagement.api.User
import com.laughing.lib.utils.SPUtil
import com.laughing.lib.utils.runInBg
import com.laughing.lib.utils.startActivity
import com.laughing.lib.utils.toJson

object UserAccountManager {
    private const val KEY_USER = "USER"

    var user: User? = null

    val uid: String
        get() {
            return user?.id ?: ""
        }

    fun saveUser(user: User?) {
        this.user = user
        if (user == null) {
            SPUtil.put(KEY_USER, "")
        } else {
            SPUtil.put(KEY_USER, user.toJson())
        }
    }

    fun isLogin(): Boolean {
        return user != null
    }

    fun logout() {
        saveUser(null)
        startActivity<LoginActivity>()
    }

    init {
        user = SPUtil.getObject<User>(KEY_USER)
    }
}